<template>
	<view class="address">
		<van-nav-bar title="收货地址" left-text="返回" left-arrow @click-left="Back" />
		<van-address-list v-model="SelectId" :list="addressList" default-tag-text="默认" @add="onAdd" @edit="onEdit"
			@select="select" />
	</view>
</template>

<script setup>
	import {
		onShow,
	} from '@dcloudio/uni-app'

	import {
		ref
	} from 'vue'

	import $api from '../../request/index.js'


	// 当前选中地址的id
	let SelectId = ref(null);
	// 地址列表
	let addressList = ref([])

	// AddressListAddress 数据结构
	// 键名	说明	类型
	// id	每条地址的唯一标识	number | string
	// name	姓名	string
	// tel	手机号	number | string
	// address	详细地址	string
	// isDefault	是否为默认地址	boolean



	onShow(() => $api.get('/moto/address/selectAddress').then(res => {
		addressList.value = res.data
		addressList.value.forEach((item, index) => {
			item.id = item.addressId
			if (item.isDefault == 1) {
				SelectId.value = item.addressId
			}
			addressList.value[index].address = item.province + item.city + item.county
		})

	}))

	// 添加
	const onAdd = () => {
		uni.navigateTo({
			url: '/pages/address_edit/address_edit'
		})
	}

	// 选择
	const select = item => SelectId.value = item.addressId


	//  修改
	const onEdit = item => {
		uni.navigateTo({
			url: '/pages/address_edit/address_edit??id=' + item.addressId
		})
	}

	// 返回
	const Back = () => uni.navigateBack()
</script>

<style>

</style>